<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"
    />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <!--jquery-->
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <!--layui-->
    <script type="text/javascript" src="./lib/layui/layui.js"></script>
    <!--cookies-->
    <script type="text/javascript" src="./js/cookies.js"></script>
    <!--spark-md5 获取文件MD5-->
    <script type="text/javascript" src="./js/spark-md5.min.js"></script>
    <!--common-->
    <script type="text/javascript" src="./js/common.js"></script>
    <!--检测登陆-->
    <script type="text/javascript" src="./js/testing_login.js"></script>
    <!--wang editor-->
    <script type="text/javascript" src="./vendor/wangEditor/release/wangEditor.min.js"></script>
    <!--xadmin js-->
    <script type="text/javascript" src="./js/xadmin.js"></script>
</head>

<body>
    <div class="x-body">
        <form class="layui-form">
            <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>序列号
                </label>
            <div class="layui-form-item">
                <div class="" id="PNTextArea">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">
                    <span class="x-red">*</span>所属商品
                </label>
                <div class="layui-input-inline">
                    <select name="belong" required="">
                        
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="add" lay-submit>
                    增加
                </button>
                <a class="layui-btn layui-btn-warm" href="javascript:location.replace(location.href);" title="重置表单">
                    重置表单
                </a>
            </div>
        </form>
    </div>
    

    <script type="text/javascript">
        var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
        var ps_number_array = [];

        layui.use(['form', 'layer'], function(){
            var form = layui.form, layer = layui.layer;
            //富文本
            var WEditor = window.wangEditor,
                editorTextArea= new WEditor('#PNTextArea');
            editorTextArea.create();

            var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };  

            function getXuNi(p_type){
                /*
                    获取虚拟商品
                    接口get 
                    p_type = Xu_Ni_CP
                */
                var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
                var _ajaxData = $.extend( {pType: p_type}, ajaxData);

                $.ajax({
                    url: BASE_SERVICEURL + URlS.product,
                    type: 'GET',
                    data: _ajaxData,
                    success: function(responseData){
                        if(responseData.msg.code == 200){
                            //console.log(responseData);
                            var belong = $('select[name="belong"]');
                            responseData.data.map(function(item, i){
                                belong.append('<option value="'+ item.pId +'">'+ item.pName['tw'] +'</option>')
                            })
                            form.render()
                            layer.close(loadIndex);
                        }else{
                            layer.close(loadIndex);
                            layer.msg(responseData.msg.msg, {time: 2000});
                        }
                    },
                    error: function(responseData){
                        layer.close(loadIndex);
                        layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
                            //跳转500
                            location.href = './500.html';
                        }});
                    }
                });
            }

            function addOldPNInto(data){
                /*
                    添加
                */
                var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
                var _ajaxData = $.extend( data, ajaxData);

                $.ajax({
                    url: BASE_SERVICEURL + URlS.lpnadd,
                    type:'GET',
                    data: _ajaxData,
                    success: function(r){
                        if(r.msg.code == 200){
                            layer.close(loadIndex);
                            console.log(r)
                        }else{
                            layer.close(loadIndex);
                            layer.msg(responseData.msg.msg, {time: 2000});
                        }
                    },
                    error: function(){
                        layer.close(loadIndex);
                        layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
                            //跳转500
                            location.href = './500.html';
                        }});
                    }
                })
            }

            form.on('submit(add)', function(data){
                var html = $.parseHTML(editorTextArea.txt.html())[1]
                //console.log(data.field)
                var tr = $(html).find('tbody tr')
                $.each(tr, function(i, val){
                    ps_number_array.push($(val).text().slice(3, 11))
                })
                //console.log(ps_number_array)
                var f = data.field;
                addOldPNInto({
                    pId: f.belong,
                    code: ps_number_array.join(';')
                })
                return false;
            })

            getXuNi('Xu_Ni_CP')
        })
    </script>
</body>

</html>